﻿import QtQuick 2.0
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import "../components"

Window {

    id: root
    title: "数据统计"
    modality: Qt.WindowModal
    flags: Qt.Window | Qt.WindowTitleHint |Qt.WindowSystemMenuHint | Qt.WindowCloseButtonHint

    property int maxRunningTime: 1000
    property int maxTestTime:    1000
    property int maxExpNum:      1000
    property int maxTestNum:     1000

    minimumHeight: 600*dp
    maximumHeight: 600*dp
    minimumWidth: 1000*dp
    maximumWidth: 1000*dp

    BoxWarning{
        id: boxwarning_exceedLimit
        box_text: "荧光检测头使用次数超过10000次，请及时检修！如有问题，请联系技术支持！"
    }

    property var tableTitle: ["设备名称", "IP地址/串口号", "设备运行时长(h)", "检测时长(h)", "实验次数", "总检测样本数"]
    property var filter: ({
                              "EquName": "",
                              "Ip": "",
                              "RunningTimeBegin": "0",
                              "RunningTimeEnd": "100000",
                              "TestTimeBegin": "0",
                              "TestTimeEnd": "100000",
                              "ExpNumBegin": "0",
                              "ExpNumEnd": "100000",
                              "TestSampleNumBegin": "0",
                              "TestSampleNumEnd": "100000"
                          })


    BoxDataStatisticsFilter{
        id: boxDataStatisticsFilter
        onAccept: {
            refresh()
        }
    }
    PushButton{
        title: "筛选"
        defaultMode1: true
        x: root.width - 140*dp
        y: 20*dp
        onReleased: {
            boxDataStatisticsFilter.show()
        }
    }

    function updateData(){
        // 模拟数据
        var res = [{   "item1": 1,
                       "item2": 2,
                       "item3": 3,
                       "item4": 4,
                       "item5": 5,
                       "item6": 6,
                   }]
        listModel.clear()
        for(let i = 0; i < res.length; i ++){
            listModel.append({
                                 "item1": res[i]["item1"],
                                 "item2": res[i]["item2"],
                                 "item3": res[i]["item3"],
                                 "item4": res[i]["item4"],
                                 "item5": res[i]["item5"],
                                 "item6": res[i]["item6"],
                             })
        }



    }
    function refresh(){
        var data = CSetUp.getStaticNumInfo(filter)
        listModel.clear()
        for(let i = 0; i < data.length; i ++){
            listModel.append({
                                 "item1": data[i]["EquName"],
                                 "item2": data[i]["Ip"],
                                 "item3": data[i]["RunningTime"],
                                 "item4": data[i]["TestTime"],
                                 "item5": data[i]["ExpNum"],
                                 "item6": data[i]["TestSampleNum"],
                             })
        }


    }

    onVisibleChanged: {
        if(visible){
//            updateData()
            refresh()
        }
    }


    Row{            // 表头 ================================================================
        y: 80*dp
        x: 20*dp
        width: root.width - 40*dp
        spacing: 0
        height: 60*dp
        Repeater{
            model: 6
            Rectangle{
                height: 60*dp
                width: (root.width - 40*dp) / 6
                color: "#F7F8FA"
                Text{
                    text: tableTitle[index]
                    font.pixelSize: config.fontsize
                    anchors.centerIn: parent
                    font.bold: true
                }
            }
        }
    }


    ListView{       // 表格 ===============================================================
        id: listView
        model: listModel
        x: 20*dp
        y: 140*dp
        height: parent.height - 200*dp
        width: root.width - 40*dp
        clip: true
        delegate: listComponent

        ScrollBar.vertical: ScrollBar{}
    }
    Text{
        text: "*"
        color: "red"
//        x: 20*dp;  y: Math.min(root.height-45*dp, listModel.count*50*dp+150*dp)
        x: 20*dp;  y: root.height-45*dp
        font.pixelSize: config.fontsize
    }
    Text{
        font.pixelSize: config.fontsize
//        x: 30*dp;  y: Math.min(root.height-45*dp, listModel.count*50*dp+150*dp)
        x: 30*dp;  y: root.height-45*dp
        text: "注: 请检查红色预警信息并根据指导方案对设备进行维护保养"
        color: "#888888"
    }

    Text{
        anchors.horizontalCenter: listView.horizontalCenter
        y: 180*dp
        text: "当前筛选内容为空"
        color: "#999999"
        font.pixelSize: config.fontsize
        visible: listModel.count === 0
    }

    Component{
        id: listComponent
        Rectangle{
            color: "#EBEDF0"
            height: 50*dp
            width: root.width - 40*dp
            Row{
                x: 1
                spacing: 1
                Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-1
                    Text{
                        text: item1
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-1
                    Text{
                        text: item2
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                }Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-1
                    Text{
                        text: item3
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                    Image{
                        height: config.fontsize
                        width: config.fontsize
                        anchors.centerIn: parent
                        anchors.horizontalCenterOffset: parent.children[0].text.length * 4.5*dp+12*dp
                        visible: Number(parent.children[0].text) > maxRunningTime
                        source: "qrc:/picture/msgWarning.png"
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onReleased: {
                                boxwarning_exceedLimit.box_text = "设备的运行时长已超过"+maxRunningTime+"(h)，请及时检修！如有问题，请联系技术支持！"
                                boxwarning_exceedLimit.show()
                            }
                        }
                    }
                }Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-1
                    Text{
                        text: item4
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                    Image{
                        height: config.fontsize
                        width: config.fontsize
                        anchors.centerIn: parent
                        anchors.horizontalCenterOffset: parent.children[0].text.length * 4.5*dp+12*dp
                        visible: Number(parent.children[0].text) > maxTestTime
                        source: "qrc:/picture/msgWarning.png"
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onReleased: {
                                boxwarning_exceedLimit.box_text = "设备的检测时长已超过"+maxTestTime+"(h)，请及时检修！如有问题，请联系技术支持！"
                                boxwarning_exceedLimit.show()
                            }
                        }
                    }
                }Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-1
                    Text{
                        text: item5
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                    Image{
                        height: config.fontsize
                        width: config.fontsize
                        anchors.centerIn: parent
                        anchors.horizontalCenterOffset: parent.children[0].text.length * 4.5*dp+12*dp
                        visible: Number(parent.children[0].text) > maxExpNum
                        source: "qrc:/picture/msgWarning.png"
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onReleased: {
                                boxwarning_exceedLimit.box_text = "设备的实验次数已超过"+maxExpNum+"次，请及时检修！如有问题，请联系技术支持！"
                                boxwarning_exceedLimit.show()
                            }
                        }
                    }
                }Rectangle{
                    height: 50*dp - 1
                    width: (root.width-40)/6-2
                    Text{
                        text: item6
                        font.pixelSize: config.fontsize
                        anchors.centerIn: parent
                    }
                    Image{
                        height: config.fontsize
                        width: config.fontsize
                        anchors.centerIn: parent
                        anchors.horizontalCenterOffset: parent.children[0].text.length * 4.5*dp+12*dp
                        visible: Number(parent.children[0].text) > maxTestNum
                        source: "qrc:/picture/msgWarning.png"
                        MouseArea{
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            onReleased: {
                                boxwarning_exceedLimit.box_text = "荧光检测头使用次数超过"+maxTestNum+"次，请及时检修！如有问题，请联系技术支持！"
                                boxwarning_exceedLimit.show()
                            }
                        }
                    }
                }
            }
        }
    }

    ListModel{
        id: listModel
    }

}
